package com.cabd.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import com.cabd.domain.PredictRow;
import com.cabd.service.ShowInfo;
import com.cabd.staticfinal.PythonFilePath;
import com.cabd.utils.CSVUtils;

public class ShowInfoImpl implements ShowInfo {

	@Override
	public String getFileType(String str) {
		String []tempStr = str.split("\\.");
		return tempStr[tempStr.length-1];
	}

	@Override
	public ArrayList<PredictRow> showPredictCSV(String path, Integer begin, Integer end) {
		ArrayList<String> arrayList = CSVUtils.getValue(path, begin, end);
		Iterator<String> iterator = arrayList.iterator();
		ArrayList<PredictRow> aListPredict = new ArrayList<>();
		while(iterator.hasNext()) {
			String string = iterator.next();
			String []temp = string.split(",");
			PredictRow predictRow = new PredictRow();
			Integer flag = 0;
			if(temp.length>5) {
				if(temp[0]!=null) {
					predictRow.setFlightno(temp[0]);
					flag++;
				}
				if (temp[1]!=null) {
					predictRow.setFlightDepcode(temp[1]);
					flag++;
				}
				if(temp[2]!=null) {
					predictRow.setFlightArrcode(temp[2]);
					flag++;
				}
				if(temp[3]!=null) {
					predictRow.setPlannedDeptime(temp[3]);
					flag++;
				}
				if(temp[4]!=null) {
					predictRow.setPlannedArrtime(temp[4]);
					flag++;
				}
				if(temp[5]!=null) {
					predictRow.setProb(temp[5]);
					flag++;
				}
			}
			if(flag==6) {
				aListPredict.add(predictRow);
			}else {
				break;
			}
		}
		return aListPredict;
	}

	@Override
	public ArrayList<String> showScource(String path) throws IOException {
		
		ArrayList<String> aList = new ArrayList<>();
		
		File file = new File(path);
		
		BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
		
		String string = "";
		
		while((string = bufferedReader.readLine())!=null) {
			System.out.println(string);
			if(string != null)
				aList.add(string);
		}
		bufferedReader.close();
		return aList;
	}
	public static void main(String[] args) {
		try {
			new ShowInfoImpl().showScource(PythonFilePath.SourceFileSk);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
